package com.yunkeji.api.user.controller;

import com.yunkeji.api.auth.vo.ToLoginVo;
import com.yunkeji.api.user.bo.FriendsListBo;
import com.yunkeji.api.user.bo.UpdateUserInfoBo;
import com.yunkeji.api.user.service.AppUserInfoService;
import com.yunkeji.api.user.vo.FriendsList;
import com.yunkeji.common.annotation.RepeatSubmit;
import com.yunkeji.common.core.controller.BaseController;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.domain.R;
import com.yunkeji.common.core.domain.model.LoginUser;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.enums.app.AccountTypeEnums;
import com.yunkeji.common.helper.LoginHelper;
import com.yunkeji.common.utils.NameUtils;
import com.yunkeji.config.annotation.ApiAuth;
import com.yunkeji.dao.domain.BaseId;
import com.yunkeji.dao.vo.AppUserWalletVo;
import com.yunkeji.dao.vo.UserVisitVo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户相关接口-个人信息
 *
 * @author 老张
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/user")
public class AppUserInfoController extends BaseController {

    private final AppUserInfoService appUserInfoService;

    /**
     * index/helloContent 获取个人信息-查自己
     *
     * @author 老张
     */
    @PostMapping("/getMineUserInfo")
    @ApiAuth(allow = {AccountTypeEnums.客服, AccountTypeEnums.用户})
    public R<ToLoginVo> getMineUserInfo() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        return R.ok(appUserInfoService.getUserInfoById(loginUser.getUserId()));
    }

    /**
     * 获取个人信息-查别人
     *
     * @author 老张
     */
    @PostMapping("/getOthersUserInfo")
    @ApiAuth(allow = {AccountTypeEnums.客服, AccountTypeEnums.用户})
    public R<ToLoginVo> getOthersUserInfo(@RequestBody @Valid BaseId id) {
        ToLoginVo userInfoById = appUserInfoService.getUserInfoById(id.getId());
        userInfoById.setUserWalletVo(null);
        return R.ok(userInfoById);
    }

    /**
     * 更新个人信息
     *
     * @author 老张
     */
    @PostMapping("/updateUserInfo")
    public R<Void> updateUserInfo(@Valid @RequestBody UpdateUserInfoBo bo) {
        appUserInfoService.updateUserInfo(bo);
        return R.ok();
    }

    /**
     * 获取钱包信息
     *
     * @author 老张
     */
    @PostMapping("/getWalletInfo")
    public R<AppUserWalletVo> getWalletInfo() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        return R.ok(appUserInfoService.getWalletInfoById(loginUser.getUserId()));
    }

    /**
     * 访客记录-最近查看
     *
     * @author 老张
     */
    @PostMapping("/visitList")
    public TableDataInfo<UserVisitVo> visitList(@RequestBody PageQuery query) {
        return appUserInfoService.visitList(query);
    }

    /**
     *
     * 列表-我关注的-关注我的
     * @author 老张
     */
    @PostMapping("/friendsList")
    public TableDataInfo<FriendsList> friendsList(@RequestBody @Valid FriendsListBo bo) {
        return appUserInfoService.friendsList(bo);
    }

    /**
     * 亲密度排序
     *
     * @author 老张
     */
    @PostMapping("/sortedLoveValue")
    public TableDataInfo<FriendsList> sortedLoveValue(@RequestBody PageQuery pageQuery) {
        return appUserInfoService.sortedLoveValue(pageQuery);
    }

    /**
     * 获取昵称列表
     *
     * @author 老张
     */
    @PostMapping("/getNames")
    public R<List<String>> getNames() {
        List<String> nickNames = NameUtils.getNickNames(12);
        return R.ok(nickNames);
    }

    /**
     * 关注与取消关注
     *
     * @author 老张
     */
    @PostMapping("/focus")
    public R focus(@RequestBody BaseId uid) {
        appUserInfoService.focus(uid.getId());
        return R.ok();
    }
}
